﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MSDS_Create
	Inherits System.Web.UI.UserControl
	Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "EVENT CONTROL-PAGE"
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not IsPostBack Then
			Me.EnableViewState = False
			Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
			If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
				ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
			Else
				Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
			End If
		End If
	End Sub

	Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
		'check if item and vendor exist in system
		Using data As New VSEntities
			Try
                Dim chk = (From q In data.MSDSOriginals Where q.ItemCode = txtItemcode.Text.Trim And q.VendorCode = hidVendorCode.Text.Trim Select q).FirstOrDefault
				If Not chk Is Nothing Then
					Excute_Javascript("AlertboxRedirect('Đã tồn tại MSDS trong hệ thống với Itemcode và Vendor này','../../Page/MSDS/Create.aspx');", Me.Page, True)
					Exit Sub
				End If

				Dim p As New MSDSOriginal
                Dim _error As String = ""
                With p
                    .ItemCode = txtItemcode.Text.Trim
                    .ItemName = txtItemname.Text.Trim
                    .VendorCode = hidVendorCode.Text
                    .Vendorname = txtVendorname.Text.Trim
                    .Original = txtOriginal.Text.Trim
                    .OriginalFile = hidImagePath.Text
                    .TraslateFile = hidImagePath2.Text
                    .URL = txtURL.Text
                    .Creator = Session("UserName")
                    .Created = Now
                    .StatusId = 1
                    .PPE = txtPPE.Text
                End With

                'Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)

                data.MSDSOriginals.AddObject(p)
                data.SaveChanges()

                Insert_App_Log("Insert  MSDSOriginal:" & txtItemname.Text.Trim & "", Function_Name.MSDSOriginal, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công." & _error & "','../../Page/MSDS/List.aspx');", Me.Page, True)
            Catch ex As Exception
                ''sc.Dispose()
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("ReloadFail();", Me.Page, True)
            Finally
                GC.Collect()
            End Try
        End Using
    End Sub
    Protected Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class